from fabric.api import *
from fabtools import require
import fabtools
env.key_filename = [
    # '~/.ssh/id_rsa',
    # '~/.ssh/singkong-aws-test.pem',
    '/Users/bopo/vagrant/debian10/.vagrant/machines/default/virtualbox/private_key'
]

env.passwords = {
    'ubuntu@49.232.52.207:22': '(|a@;~QZSU6ALu2',
}
# env.hosts = ['vagrant@127.0.0.1:2222']

env.roledefs = {
    'dev': {
        'hosts': ['vagrant@127.0.0.1:2222'],
    },
    'pre': {
        'hosts': ['pi@raspberrypi'],
    },
    'rel': {
        'hosts': ['ubuntu@49.232.52.207'],
    }
}

@task
def setup():

    # Require some Debian/Ubuntu packages
    require.deb.packages([
        'imagemagick',
        'libxml2-dev',
    ])

    # Require a Python package
    with fabtools.python.virtualenv('/home/myuser/env'):
        require.python.package('pyramid')

    # Require an email server
    require.postfix.server('example.com')

    # Require a PostgreSQL server
    require.postgres.server()
    require.postgres.user('myuser', 's3cr3tp4ssw0rd')
    require.postgres.database('myappsdb', 'myuser')

    # Require a supervisor process for our app
    require.supervisor.process('myapp',
        command='/home/myuser/env/bin/gunicorn_paster /home/myuser/env/myapp/production.ini',
        directory='/home/myuser/env/myapp',
        user='myuser'
        )

    # Require an nginx server proxying to our app
    require.nginx.proxied_site('example.com',
        docroot='/home/myuser/env/myapp/myapp/public',
        proxy_url='http://127.0.0.1:8888'
        )

    # Setup a daily cron task
    fabtools.cron.add_daily('maintenance', 'myuser', 'my_script.py')